/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.controller;

import com.mavenrentcars.exception.ViewException;
import com.mavenrentcars.form.CarForm;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Place;
import com.mavenrentcars.service.AccountService;
import com.mavenrentcars.service.CarService;
import com.mavenrentcars.service.PlaceService;
import com.mavenrentcars.validator.Validator;
import com.mavenrentcars.validator.impl.CarValidator;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author serg
 */
@Controller
public class CarController {

    @Autowired
    AccountService accountService;
    @Autowired
    CarService carService;
    @Autowired
    PlaceService placeService;

    @RequestMapping(value = "my_cars_list.htm", method = RequestMethod.GET)
    public ModelAndView myCars(
            HttpServletResponse httpServletResponse) {

        ModelAndView mav = new ModelAndView("car/list");
        try {
            List<Car> list = carService.getAllByUser();
            mav.addObject("list", list);
        } catch (Exception ex) {
            Logger.getLogger(CarController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mav.addObject("acc", accountService);
        return mav;
    }

    @RequestMapping(value = "car_item.htm", method = RequestMethod.GET)
    public ModelAndView item(
            HttpServletResponse httpServletResponse,
            @RequestParam(value = "id", required = true) int id) {
        ModelAndView mav = new ModelAndView("car/item");
        mav.addObject("acc", accountService);



        try {
            CarForm form = new CarForm(carService.get(id));
            form.setAction("edit");
            List<Place> places = placeService.getAll();
            String json_places = placeService.getJsonPlaces();
            mav.addObject("item", form);
            mav.addObject("places", places);
            mav.addObject("json_places", json_places);
        } catch (ViewException ex) {
            Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }

        mav.addObject("acc", accountService);
        mav.addObject("res", true);
        return mav;
    }

    @RequestMapping(value = "car_view.htm", method = RequestMethod.GET)
    public ModelAndView view(
            HttpServletResponse httpServletResponse) {
        ModelAndView mav = new ModelAndView("car/item");
        mav.addObject("acc", accountService);

        CarForm form = new CarForm();
        try {
            List<Place> places = placeService.getAll();
            String json_places = placeService.getJsonPlaces();
            mav.addObject("places", places);
            mav.addObject("json_places", json_places);
        } catch (ViewException ex) {
            Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mav.addObject("item", form);
        mav.addObject("acc", accountService);
        mav.addObject("res", true);
        return mav;
    }

    @RequestMapping(value = "car_add.htm", method = RequestMethod.POST)
    public ModelAndView add(
            HttpServletResponse httpServletResponse,
            @RequestParam(value = "data", required = true) String data) {

        ObjectMapper mapper = new ObjectMapper();
        ModelAndView mav = new ModelAndView("jsonView");

        CarForm form;
        try {
            form = mapper.readValue(data, CarForm.class);
            Validator valid = new CarValidator(form);
            if (!valid.validation()) {
                mav.addObject("res", false);
                mav.addObject("errors", valid.getErrors());
                return mav;
            }

            carService.add(form);
            mav.addObject("res", true);
            mav.addObject("item", form);
        } catch (ViewException ex) {
            mav.addObject("error", ex.getMessage());
            mav.addObject("res", false);
        } catch (JsonGenerationException ex) {
            mav.addObject("error", ex.getMessage());
            mav.addObject("res", false);
        } catch (JsonMappingException ex) {
            mav.addObject("error", ex.getMessage());
            mav.addObject("res", false);
        } catch (IOException ex) {
            mav.addObject("error", ex.getMessage());
            mav.addObject("res", false);
        }

        return mav;
    }
}
